{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Publishing SDs, Shapefiles and CSVs\n",
    "\n",
    "Publishing your data can be accomplished in two simple steps:\n",
    "1. Add the local data as an item to the portal\n",
    "2. Call the publish() method on the item\n",
    "\n",
    "This sample notebook shows how different types of GIS datasets can be added to the GIS, and published as web layers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Publishing-SDs,-Shapefiles-and-CSVs\" data-toc-modified-id=\"Publishing-SDs,-Shapefiles-and-CSVs-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Publishing SDs, Shapefiles and CSVs</a></span></li><li><span><a href=\"#Publish-all-the-service-definition-files-in-a-folder\" data-toc-modified-id=\"Publish-all-the-service-definition-files-in-a-folder-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Publish all the service definition files in a folder</a></span></li><li><span><a href=\"#Publish-a-feature-service-from-a-shapefile-and-update-the-item-information\" data-toc-modified-id=\"Publish-a-feature-service-from-a-shapefile-and-update-the-item-information-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>Publish a feature service from a shapefile and update the item information</a></span><ul class=\"toc-item\"><li><ul class=\"toc-item\"><li><span><a href=\"#Update-the-layer-item's-metadata\" data-toc-modified-id=\"Update-the-layer-item's-metadata-3.0.1\"><span class=\"toc-item-num\">3.0.1&nbsp;&nbsp;</span>Update the layer item's metadata</a></span></li></ul></li></ul></li><li><span><a href=\"#Publish-a-CSV-file-and-move-it-into-a-folder\" data-toc-modified-id=\"Publish-a-CSV-file-and-move-it-into-a-folder-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>Publish a CSV file and move it into a folder</a></span><ul class=\"toc-item\"><li><ul class=\"toc-item\"><li><span><a href=\"#Create-a-new-folder-for-the-items\" data-toc-modified-id=\"Create-a-new-folder-for-the-items-4.0.1\"><span class=\"toc-item-num\">4.0.1&nbsp;&nbsp;</span>Create a new folder for the items</a></span></li></ul></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display\n",
    "from arcgis.gis import GIS\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to GIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gis = GIS('Home')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Remove existing content\n",
    "\n",
    "For this illustration, in order to have the code and data operations below run smoothly, we'll use the cells below to delete existing .zip, .sd, or services from the `gis` content and delete the existing folder that could cause conflict during publishing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Deleted existing Service Definition:  <Item title:\"Nursing_home_locations\" type:Service Definition owner:arcgis_python>\n",
      "Deleted existing Service Definition:  <Item title:\"NewPy_WTL_test_SingleLayerBuildCache\" type:Service Definition owner:arcgis_python>\n",
      "Deleted existing Feature Layer Collection:  <Item title:\"Nursing_home_locations\" type:Feature Layer Collection owner:arcgis_python>\n",
      "Deleted existing Map Service:  <Item title:\"NewPy_WTL_test_SingleLayerBuildCache\" type:Map Image Layer owner:arcgis_python>\n"
     ]
    }
   ],
   "source": [
    "def delete_existing_items(item_types, name_list):\n",
    "    for current_item_type in item_types:\n",
    "        for file_name in name_list:    \n",
    "            search_result = gis.content.search(query=file_name, item_type=current_item_type)\n",
    "            if len(search_result) > 0:\n",
    "                for item in search_result:\n",
    "                    item.delete(permanent=True) # if recycle bin is enabled, otherwise the parameter is ignored\n",
    "                    print(\"Deleted existing \" + current_item_type + \": \", item)\n",
    "                    \n",
    "item_types = [\"Service Definition\", \"Feature Layer Collection\", \"Map Service\"]\n",
    "name_list = [\"Nursing_home_locations\", \"NewPy_WTL_test_SingleLayerBuildCache\"]\n",
    "delete_existing_items(item_types, name_list)\n",
    "\n",
    "item_types = [\"Shapefile\", \"Feature Layer Collection\"]\n",
    "name_list = [\"power_pedestals_2012\"]\n",
    "delete_existing_items(item_types, name_list)\n",
    "\n",
    "item_types = [\"CSV\", \"Feature Layer Collection\"]\n",
    "name_list = [\"Chennai_precipitation\"]\n",
    "delete_existing_items(item_types, name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def delete_existing_folder(folder_name):\n",
    "    try:\n",
    "        folder = gis.content.folders.get(folder_name)\n",
    "        return folder.delete(permanent=True) # if recycle bin is enabled, otherwise the parameter is ignored\n",
    "    except:\n",
    "        return False\n",
    "\n",
    "my_folder_name = \"Rainfall Data\"\n",
    "delete_existing_folder(my_folder_name) # returns True if folder exists, or False if non-exist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Publish all the service definition files in a folder\n",
    "\n",
    "The sample below lists all the service definition (.sd) files in a data directory and publishes them as web layers. To publish a service definition file, we first add the .sd file to the Portal, and then call the publish() method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of .sd files found: 2\n"
     ]
    }
   ],
   "source": [
    "# path relative to this notebook\n",
    "data_dir = \"data/publishing_sd_shapefiles_and_csv/\"\n",
    "\n",
    "#Get list of all files\n",
    "file_list = os.listdir(data_dir)\n",
    "\n",
    "#Filter and get only .sd files\n",
    "sd_file_list = [x for x in file_list if x.endswith(\".sd\")]\n",
    "print(\"Number of .sd files found: \" + str(len(sd_file_list)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=887e0688234e4ebb85578013da5a46a8' target='_blank'>\n",
       "                        <img src='http://static.arcgis.com/images/desktopapp.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=887e0688234e4ebb85578013da5a46a8' target='_blank'><b>NewPy_WTL_test_SingleLayerBuildCache</b>\n",
       "                        </a>\n",
       "                        <br/><img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/mapimages16.png' style=\"vertical-align:middle;\">Map Image Layer by arcgis_python\n",
       "                        <br/>Last Modified: August 27, 2020\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"NewPy_WTL_test_SingleLayerBuildCache\" type:Map Image Layer owner:arcgis_python>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=c6aea4088d4648758ae27309b66c2e09' target='_blank'>\n",
       "                        <img src='' width='200' height='133' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=c6aea4088d4648758ae27309b66c2e09' target='_blank'><b>Nursing_home_locations</b>\n",
       "                        </a>\n",
       "                        <br/>Presents the locations of Nursing Homes for Department of Social and Health Services, Wasington State.<img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by arcgis_python\n",
       "                        <br/>Last Modified: August 27, 2020\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Nursing_home_locations\" type:Feature Layer Collection owner:arcgis_python>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "root_folder = gis.content.folders.get()\n",
    "# Loop through each file and publish it as a service\n",
    "for current_sd_file in sd_file_list:\n",
    "    item = root_folder.add(\n",
    "        item_properties={\n",
    "            \"title\": \"New Layer\", \n",
    "            \"type\": \"Service Definition\"\n",
    "        }, \n",
    "        file=data_dir + current_sd_file).result()   # .sd file is uploaded and a .sd file item is created\n",
    "    if \"BuildCache\" not in current_sd_file:\n",
    "        published_item = item.publish()   # .sd file item is published and a web layer item is created\n",
    "    else:\n",
    "        published_item = item.publish(build_initial_cache=True)  # publish as hosted tile layer with \"build cache\" enabled\n",
    "    display(published_item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the example shown above, one .sd file produced a web feature layer and another produced a web tile layer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Note**: When publishing an SD file to Enterprise with customized `publish_parameters`, as specified in [REST API](https://developers.arcgis.com/rest/users-groups-and-items/publish-item.htm), the customization of item properties, such as title, snippets, tags, and description, will not be reflected in the properties of online SD file, or the published service. Instead, the SD file item, and the service item will contain the local SD file's original settings. A recommended workflow is to use `update()` method after uploading the SD file, and publishing the service."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we'll connect to an ArcGIS Enterprise deployment and publish a service definition file and illustrate the updating of item properties published from them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p_gis = GIS(\"your_enterprise_profile\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "sd_file = \"data/publishing_sd_shapefiles_and_csv/Nursing_home_locations.sd\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's add the service definition file item:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['opendata', 'health', 'automation', 'ArcGIS', 'Service Definition', '.sd']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "proot_folder = p_gis.content.folders.get()\n",
    "uploaded_file = proot_folder.add({\n",
    "    \"title\": \"Nursing Home Location\", \n",
    "    \"type\": \"Service Definition\"}, file=sd_file).result()\n",
    "uploaded_file.tags"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll update some of the item properties for the service definition file by defining a dictionary and inputting it the [Item.update()](/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update) method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "item_properties = {\n",
    "    \"title\": \"Nursing Home Locations from SD file\",\n",
    "    \"tags\" : uploaded_file.tags + [\"test 1st\", \"test 2nd\", \"test 2023\"],\n",
    "    \"snippet\": \"Nursing homes in Washington state added in API\",\n",
    "    \"description\": \"Locations of nursing homes in US state of Washington\",\n",
    "    \"type\": \"Feature Service\",\n",
    "    \"typeKeywords\": \"Data, Feature Service, Hosted Service, Service, Singlelayer\", \n",
    "    'access': 'org'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['opendata',\n",
       " 'health',\n",
       " 'automation',\n",
       " 'ArcGIS',\n",
       " 'Service Definition',\n",
       " '.sd',\n",
       " 'test 1st',\n",
       " 'test 2nd',\n",
       " 'test 2023']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uploaded_file.update(item_properties) # this procedure overwrites the properties you set in sd file\n",
    "uploaded_file.tags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['opendata', 'health', 'automation', 'ArcGIS', 'Service Definition', '.sd']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Publish the service from the service definition item\n",
    "item = uploaded_file.publish()\n",
    "item.tags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['opendata',\n",
       " 'health',\n",
       " 'automation',\n",
       " 'ArcGIS',\n",
       " 'Service Definition',\n",
       " '.sd',\n",
       " 'test 1st',\n",
       " 'test 2nd',\n",
       " 'test 2023']"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item.update(item_properties) # this procedure overwrites the properties you set in service\n",
    "item.tags"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `update()` called after SD file uploading, and SD item being published into web service, the item properties have adopted the user customization (and modified the original settings in the local SD file)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Recycle bin not enabled on this organization. Permanent delete parameter ignored.\n",
      "Recycle bin not enabled on this organization. Permanent delete parameter ignored.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item.delete(permanent=True)\n",
    "uploaded_file.delete(permanent=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Publish a feature service from a shapefile and update the item information\n",
    "\n",
    "To publish a shapefile, we first add the zipped shapefile to the Portal as an item, then call publish() method on the item to create a web layer. Often times, your shape files or service definitions may not contain the metadata you want to show on the portal item. This sample demonstrates how you can update those properties after publishing a web layer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = \"data/power_pedestals_2012.zip\"\n",
    "shpfile = root_folder.add({\"title\": \"power_pedestals_2012\", \"type\": \"Shapefile\"}, data).result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=07faab47b04043a2b14fd42af4fd1c98' target='_blank'>\n",
       "                        <img src='http://static.arcgis.com/images/desktopapp.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=07faab47b04043a2b14fd42af4fd1c98' target='_blank'><b>power_pedestals_2012</b>\n",
       "                        </a>\n",
       "                        <br/><img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/datafiles16.png' style=\"vertical-align:middle;\">Shapefile by arcgis_python\n",
       "                        <br/>Last Modified: August 27, 2020\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"power_pedestals_2012\" type:Shapefile owner:arcgis_python>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shpfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "published_service = shpfile.publish()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=56d858ae0833439bbdc84ce5a826a8f1' target='_blank'>\n",
       "                        <img src='http://static.arcgis.com/images/desktopapp.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=56d858ae0833439bbdc84ce5a826a8f1' target='_blank'><b>power_pedestals_2012</b>\n",
       "                        </a>\n",
       "                        <br/><img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by arcgis_python\n",
       "                        <br/>Last Modified: August 27, 2020\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"power_pedestals_2012\" type:Feature Layer Collection owner:arcgis_python>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(published_service)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The web layer item has minimal information and a default thumbnail.\n",
    "\n",
    "### Update the layer item's metadata\n",
    "To update the metadata and set the thumbnail, use the update() method on the web layer's item obtained during publishing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "thumbnail_path = \"data/power_pedestals_thumbnail.PNG\"\n",
    "item_properties = {\"snippet\":\"\"\"This dataset was collected from Utah DOT open data portal.\n",
    "                            Source URL: <a href=\"http://udot.uplan.opendata.arcgis.com/\n",
    "                            datasets/a627bb128ac44767832402f7f9bde909_10\">http://udot.uplan.opendata.arcgis.com/\n",
    "                            datasets/a627bb128ac44767832402f7f9bde909_10</a>\"\"\",\n",
    "                   \"title\":\"Locations of power pedestals collected in 2012\",\n",
    "                   \"tags\":\"opendata\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=56d858ae0833439bbdc84ce5a826a8f1' target='_blank'>\n",
       "                        <img src='' width='200' height='133' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=56d858ae0833439bbdc84ce5a826a8f1' target='_blank'><b>Locations of power pedestals collected in 2012</b>\n",
       "                        </a>\n",
       "                        <br/>This dataset was collected from Utah DOT open data portal.\n",
       "                            Source URL: <a href='http://udot.uplan.opendata.arcgis.com/\n",
       "                            datasets/a627bb128ac44767832402f7f9bde909_10' rel='nofollow ugc'>http://udot.uplan.opendata.arcgis.com/\n",
       "                            datasets/a627bb128ac44767832402f7f9bde909_10</a><img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by arcgis_python\n",
       "                        <br/>Last Modified: August 27, 2020\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Locations of power pedestals collected in 2012\" type:Feature Layer Collection owner:arcgis_python>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "published_service.update(item_properties, thumbnail=thumbnail_path)\n",
    "display(published_service)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Publish a CSV file and move it into a folder\n",
    "\n",
    "To publish a CSV file, we first add the .csv file to the Portal, and then call the publish() method to publish it as a layer. Once published, we create a destination folder on the server and then move the published items into that folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_file = 'data/Chennai_precipitation.csv'\n",
    "csv_item = root_folder.add({\"title\": \"Chennai Precipitation\", \"type\": \"CSV\"}, csv_file).result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=1e9d2174d58946d09d13a381ed4f33dc' target='_blank'>\n",
       "                        <img src='http://static.arcgis.com/images/desktopapp.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=1e9d2174d58946d09d13a381ed4f33dc' target='_blank'><b>Chennai_precipitation</b>\n",
       "                        </a>\n",
       "                        <br/><img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/layers16.png' style=\"vertical-align:middle;\">CSV by arcgis_python\n",
       "                        <br/>Last Modified: August 27, 2020\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Chennai_precipitation\" type:CSV owner:arcgis_python>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(csv_item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The csv file used in this sample has a column titled `LOCATION` containing place names in text. During the publishing process we specify this column as an `address_fields` parameter. The server geocodes these locations to create point features for the web layer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_lyr = csv_item.publish(None, {\"Address\":\"LOCATION\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=73276a77fe854be980a39e5fd683705f' target='_blank'>\n",
       "                        <img src='http://static.arcgis.com/images/desktopapp.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=73276a77fe854be980a39e5fd683705f' target='_blank'><b>Chennai_precipitation</b>\n",
       "                        </a>\n",
       "                        <br/><img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by arcgis_python\n",
       "                        <br/>Last Modified: August 27, 2020\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Chennai_precipitation\" type:Feature Layer Collection owner:arcgis_python>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(csv_lyr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "### Create a new folder for the items\n",
    "The `create()` from `GIS.content.folders` can be used to create a new folder. Once created, the `move()` of the `Item` can be used to move the items into the folder."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'username': 'arcgis_python', 'id': '3b6a6606e9c3460eac644c5d3d8972b2', 'title': 'Rainfall Data'}\n"
     ]
    }
   ],
   "source": [
    "# create a new folder called 'Rainfall Data'\n",
    "new_folder = gis.content.folders.create(my_folder_name)\n",
    "print(new_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'success': True,\n",
       " 'itemId': '73276a77fe854be980a39e5fd683705f',\n",
       " 'owner': 'arcgis_python',\n",
       " 'folder': '3b6a6606e9c3460eac644c5d3d8972b2'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# move both the csv_item and csv_lyr items into this new folder\n",
    "csv_item.move(new_folder)  # Here you could either pass name of the folder or the dictionary\n",
    "                                   # returned from create_folder() or folders property on a User object\n",
    "\n",
    "csv_lyr.move(new_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "Now that the items are moved, we can request for the item's `ownerFolder` property and ensure it matches the `id` of the folder created in the previous step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3b6a6606e9c3460eac644c5d3d8972b2\n"
     ]
    }
   ],
   "source": [
    "print(csv_lyr.ownerFolder)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "4.0"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
